package cn.touna.jss.modules.adapter;

import cn.touna.jss.common.entity.CreditQueryRequest;
import cn.touna.jss.common.entity.FileRequestEntity;
import cn.touna.jss.common.mq.KafkaComponent;
import cn.touna.jss.common.properties.config.SysConfigProperties;
import cn.touna.risk.api.domain.Response;
import cn.touna.risk.api.entity.RequestEntity;
import cn.touna.risk.api.enums.SystemErrorCodeEnum;
import cn.touna.risk.cache.redis.RedisCache;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * 统一入口
 * Created by wuwc on 2017/11/30.
 */
@RestController
public class GatewayController {

    private static final Logger logger = LogManager.getLogger(GatewayController.class);

    @Autowired
    private GatewayHanlder apiHandler;

    @Autowired
    private KafkaComponent kafkaComponent;

    @Autowired
    RedisCache redisCache;

    @Autowired
    SysConfigProperties sysConfigProperties;

    /**
     * 请求适配入口
     * 该接口1.5.0后兼容文件byte流上传
     *
     * @param requestEntity
     * @return
     */
    @RequestMapping(value = {"/crs/applyCredit"}, method = {RequestMethod.POST})
    public Response handleRequest(@RequestBody RequestEntity requestEntity, HttpServletRequest request) {
        long start = System.currentTimeMillis();
        requestEntity.setSystemName(request.getHeader("sysName"));
        requestEntity.setProductType(request.getHeader("productType"));
        requestEntity.setPhaseName(request.getHeader("phaseName"));
        requestEntity.setClientIp(request.getHeader("client_ip"));
        logger.info("REQ【请求参数】,params:{}", requestEntity);
        Response response = apiHandler.hanldPost(requestEntity, request);
        logger.info("RSP【返回结果】,params:{},result:{},cost:{}ms", requestEntity, response, System.currentTimeMillis() - start);
        return response;
    }

    /**
     * 文件流请求适配入口
     * 该接口除了商汤人脸识别接口进行文件上传外，，后续其他将统一使用handleRequest接口(支持文件传输方式)
     *
     * @param fileRequest
     * @param request
     * @return
     */
    @RequestMapping(value = "/crs/applyFileCredit", method = {RequestMethod.POST})
    public Response handleFileRequest(FileRequestEntity fileRequest, HttpServletRequest request) {
        long start = System.currentTimeMillis();
        fileRequest.setClientIp(request.getHeader("client_ip"));
        fileRequest.setSystemName(request.getHeader("sysName"));
        fileRequest.setProductType(request.getHeader("productType"));
        fileRequest.setPhaseName(request.getHeader("phaseName"));
        logger.info("REQ【请求参数】,params:{}", fileRequest);
        Response response = new Response();
        try {
            response = apiHandler.hanldFilePost(fileRequest, request);
        } catch (Exception e) {
            return response.fail(SystemErrorCodeEnum.JSS_SYSTEM_ERROR);
        } finally {
            CreditQueryRequest queryRequest = ApiUtils.assembleFileRequest(fileRequest);
            kafkaComponent.sendQueryHistory(queryRequest, response);
        }
        logger.info("RSP【返回结果】,params:{},result:{},cost:{}ms", fileRequest, response, System.currentTimeMillis() - start);
        return response;
    }

}
